import streamlit as st
import pymysql
from datetime import datetime
from urllib import parse
import pandas as pd
# con = pymysql.connect(host="192.168.8.23", user="user_bigData", password="9X_v4!qN1F%d", db="smart_audit")
import sqlalchemy
from sqlalchemy import text
from sqlalchemy.orm import sessionmaker, relationship
import json

pwd = parse.quote_plus('1qaz2wsx')
engine = sqlalchemy.create_engine(f'''mysql+pymysql://root:root@127.0.0.1:3306/smart_temp?charset=utf8''')

data = None

msg_dict = {
    "PASS": "通过",
    "CHECKING": "审核中",
    "PASS": "通过",
    "EXECUTE_NOT_START": "执行未开始",
    "EXECUTE_STARTING": "执行中",
    "EXECUTE_CHECKING": "执行审核中",
    "EXECUTE_REJECT": "执行审核驳回",
    "REJECT": "驳回",
}


def read_data_from_sql(sql):
    print(sql)
    Session = sessionmaker(bind=engine)
    session = Session()
    cursor = session.execute(text(sql))
    result = cursor.fetchall()
    result_dict = [row._asdict() for row in result]
    session.close()
    return result_dict


def create_app():
    sql = f'''select * from f_task where vcType ='WORK_SERVICE' limit 5 '''
    result = read_data_from_sql(sql)
    data = result
    for i, item in enumerate(data):
        with st.expander(item['vcName']):
            detail = json.loads(item['vcDetail'])
            serviceExpertCnt = detail['serviceExpertCnt']
            id = item['intId']
            print(serviceExpertCnt)
            cnt_dict = serviceExpertCnt
            col1, col2 = st.columns(spec=2)
            with col1:
                st.write("项目名称: %s" % item['vcName'])
                st.write("申请原因: %s" % detail['content'])
            with col2:
                for k, cnt_item in enumerate(cnt_dict):
                    st.text("专家资源:" + cnt_item['name'])
                    # ren = st.text_input("专家资源:", value=cnt_item['name'], key=str(i)+str(k)+"f")
                    ren = st.text_input("租用人数:", value=cnt_item['cnt'], key=str(i) + str(k))

            key_id = item['intId']
            t_sql = f'''select * from   f_task_audit_record where  intTaskId ={key_id} '''
            task_list = read_data_from_sql(t_sql)
            for j, t_item in enumerate(task_list):
                ren = st.text_input("执行人:", value=t_item['vcAuditAdminName'], key=str(j) + str(i) + 're')
                new_age = st.text_input("执行时间:", value=t_item['dtCreateTime'], key=str(j) + str(i) + 'ga')
                new_email = st.text_input("完成时间:", value=t_item['dtCreateTime'], key=str(j) + str(i) + 'fw')
                new_email = st.text_input("执行状态:", value=msg_dict[t_item['vcLastState']], key=str(j) + str(i) + 'f2w')
                print(ren)
            if st.button("确定修改", key=str(i) + 'fee'):
                # print(user.name,user.age,user.email)
                # 显示一个消息，告知用户详情已更新
                st.success("用户详情已更新！")
                st.write("修改成功", id, 'id', task_list)


def create_excel(data):
    head = ['id', '项目名称', '申请原因', '租用情况', '任务情况']
    for i, item in enumerate(data):
        row = []
        detail = json.loads(item['vcDetail'])
        serviceExpertCnt = detail['serviceExpertCnt']
        id = item['intId']
        t_sql = f'''select * from   f_task_audit_record where  intTaskId ={id} '''
        t_list = []
        for j, t_item in enumerate(task_list):
            temp = {
                '执行人': t_item['vcAuditAdminName'],
                '执行时间': t_item['dtCreateTime'],
                '执行状态': msg_dict[t_item['vcLastState']],
                    }
            t_list.append(temp)
        row = [id, item['vcName'], detail['content'], serviceExpertCnt,t_list]
        task_list = read_data_from_sql(t_sql)


if __name__ == '__main__':
    create_app()
